package com.ruoyi.function.jobhandler;


import com.ruoyi.common.utils.SecurityUtils;

import com.ruoyi.demowechatpay.enumeration.TransferDetailStatusEnum;
import com.ruoyi.demowechatpay.pojo.dto.WeChatTransferQueryDTO;
import com.ruoyi.demowechatpay.service.TransferService;
import com.ruoyi.function.constant.BusinessConstants;
import com.ruoyi.function.domain.TransferResult;
import com.ruoyi.function.mapper.TransferResultMapper;
import com.ruoyi.function.service.IUserFlowingService;
import com.ruoyi.function.service.IUserService;

import com.ruoyi.message.controller.NoticeController;
import com.ruoyi.message.notice.vo.NoticeVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;


/**
 * 定时任务
 *
 * @author zlj
 * @since 2023/8/10
 */
@Slf4j
@Component
//@EnableScheduling
public class WxOrderHandler {
    @Autowired
    private TransferResultMapper transferResultMapper;
    @Autowired
    private TransferService transferService;
    @Autowired
    private IUserService userService;
    @Autowired
    private IUserFlowingService iUserFlowingService;
    @Autowired
    private NoticeController noticeController;


    /**
     * 定时更新微信提现
     */
    @Scheduled(cron = "0 0/5 * * * ?")
    @Transactional
    public void updateOrder() {
        log.info("定时更新微信提现");
        TransferResult transferResult = new TransferResult();
        transferResult.setState(BusinessConstants.WAIT_USER_CONFIRM);
        List<TransferResult> transferResults = transferResultMapper.selectTransferResultList(transferResult);
        WeChatTransferQueryDTO weChatTransferQueryDTO = new WeChatTransferQueryDTO();
        for (TransferResult result : transferResults) {
            NoticeVO noticeVO = new NoticeVO();
            noticeVO.setReceiverId(result.getUserId() + "");
            noticeVO.setUserType(result.getUserType());
            weChatTransferQueryDTO.setTransferBillNo(result.getTransferBillNo());
            TransferDetailStatusEnum transferDetailStatusEnum = transferService.queryTransfer(weChatTransferQueryDTO);
            String label = transferDetailStatusEnum.label();
            if (BusinessConstants.SUCCESS.equals(label)){
                result.setState(BusinessConstants.SUCCESS);
                noticeVO.setMessageContent( result.getBalance()+BusinessConstants.message_4);
            }
            if (BusinessConstants.CANCELING.equals(label)){
                result.setState(BusinessConstants.CANCELING);
                // 修改用户余额
                iUserFlowingService.addAmount(result.getUserId(), result.getBalance(), BusinessConstants.dict.fee_type_3, result.getUserType(), null);
            //发送消息
                noticeVO.setMessageContent( result.getBalance()+BusinessConstants.message_2);
            }
            noticeController.add(noticeVO);
            result.setIsFinished("1");
            transferResultMapper.updateTransferResult(result);
        }


    }
}
